home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 838 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.rrz.uni-hamburg.de!rzdspc1!simmons
  2. From: simmons@rzdspc1.informatik.uni-hamburg.de (Geoffrey Simmons)
  3. Newsgroups: comp.lang.c,comp.unix.programmer,comp.databases
  4. Subject: Re: Aligning struct fields with NDBM (was: Casting unsigned short ...)
  5. Date: 9 Jan 96 10:46:17 GMT
  6. Organization: University of Hamburg -- Germany
  7. Message-ID: <simmons.821184377@rzdspc1>
  8. References: <simmons.820857453@rzdspc1> <simmons.821116795@rzdspc1> <821142071snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: rzdspc1.informatik.uni-hamburg.de
  10.  
  11. Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  12.  
  13. >While ndbm isn't part of the C language, your problem seems very much C
  14. >language related so I've reinstated comp.lang.c.
  15.  
  16. OK. (I try to be careful about littering newsgroups, esp. since I'm not a
  17. regular around here.)
  18.  
  19. I wrote:
  20. >>        if ((MyRec = (RecType *) malloc(theDatum.dsize)) == NULL)
  21.  
  22. and then:
  23. >>        MyRec = (RecType *) theDatum.dptr;
  24.  
  25. Lawrence gently points out the obvious:
  26. >You've just gone and thrown away the pointer to your malloc'd block (this is
  27. >a memory leak). Maybe you meant to copy the data in the struct in which case
  28. >you need instead:
  29.  
  30. >         memcpy(MyRec, theDatum.dptr, sizeof(RecType));
  31.  
  32. >Don't use:
  33.  
  34. >         *MyRec = *(RecType *) theDatum.dptr;
  35.  
  36. >because this may assume alignment that is not guaranteed.
  37.  
  38. This was my problem, and using memcpy() is the right solution. I must say,
  39. when my mistake is pointed out to me, I find my own silliness breathtaking.
  40. Thanks also to Chris Dodd <dodd@csl.sri.com> for pointing out this error
  41. as well by e-mail.
  42.  
  43. >>Is this a well-known problem with NDBM, with a well-known solution?
  44.  
  45. >There is nothing in your post that indicates a problem with ndbm, you
  46. >just need to fix your code.
  47.  
  48. I didn't mean to accuse NDBM; but I can imagine that others who have used
  49. it have had the same difficulties with memory alignment that I had.
  50.  
  51. While I'm here, do you know of a good way to use the debugger to get at the
  52. bottom of a problem like this (alignment errors, and possibly memory leaks)?
  53. I struggled with this mainly because the debugger (GNU GDB 4.15.1) could
  54. do everything that the running program couldn't do, so Usenet became my
  55. only hope.
  56.  
  57. Eureka!
  58. Geoff
  59. -- 
  60. Geoffrey Simmons     |  simmons@informatik.uni-hamburg.de    | "Insert wise
  61. University of Hamburg     |    Phone: (++49 40) 54715-381    |  and witty
  62. Vogt-Koelln-Str. 30     |    Fax:   (++49 40) 54715-385    |  quotation
  63. D-22527 Hamburg, Germany |                    |  here."
  64.